﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using DddCms.Infrastructure.Repository;
using System.Data.Entity.Infrastructure;
using Microsoft.Practices.Unity;
using Rhino.Mocks;
using System.Data.Objects;
using DddCms.Domain.Model;
using Ploeh.AutoFixture;
using DddCms.Domain.Utility;

namespace DddCms.Infrastructure.Test.Repository
{
    /// <summary>
    /// The data context class test
    /// </summary>
    [TestClass]
    public class DataContextTest
    {
        #region fixture
        public User Entity { get; set; }
        #endregion

        /// <summary>
        /// On crée une entity utilisateur avec des donnée
        /// aléatoire bonne
        /// </summary>
        [TestInitialize]
        public void TestInitialize()
        {
            Fixture fixture = new Fixture();
            fixture.Inject<IPasswordUtil>(MockRepository.GenerateStrictMock<IPasswordUtil>());
            fixture.Inject<ILoggingUtil>(MockRepository.GenerateStrictMock<ILoggingUtil>());
            Entity = fixture.CreateAnonymous<User>();
        }

        /// <summary>
        /// Test the good init in constructor
        /// </summary>
        [TestMethod]
        public void DataContextConstructorTest()
        {
            DataContext dataContext = new DataContext();
            Assert.IsNotNull(dataContext.PasswordUtil);
        }

        /// <summary>
        /// Test the exeption when the eventArg is null
        /// </summary>
        [TestMethod]
        public void DataContextOnObjectMaterializedTest()
        {
            IUnityContainer container = MockRepository.GenerateStrictMock<IUnityContainer>();

            container.Expect(u => u.BuildUp(Entity.GetType().BaseType, Entity));

            DataContextMockDeux dataContext = new DataContextMockDeux();

            dataContext.UnityContainer = container;

            try
            {
                dataContext.OnObjectMaterializedFake(null, null);
                Assert.IsTrue(false);
            }
            catch (Exception)
            {

                Assert.IsTrue(true);
            }

        }
        /// <summary>
        /// Test if the save methode is called
        /// </summary>
        [TestMethod]
        public void DataContextSaveTest()
        {
            DataContext dataContext = new DataContext();

            Assert.AreEqual(dataContext.Save(), 0);
        }
        /// <summary>
        /// Test if the unity build up is called
        /// </summary>
        [TestMethod]
        public void DataContextResolveInstanceTest()
        {
            IUnityContainer container = MockRepository.GenerateStrictMock<IUnityContainer>();
            container.Expect(u => u.BuildUp(null, null));

            DataContext dataContext = new DataContext();
            dataContext.UnityContainer = container;
            dataContext.ResolveInstance(null, null);

            container.VerifyAllExpectations();
        }
    }
}
